package practice05;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice05
 * @ClassName: Client
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/1/3 9:23
 * @Copyright 大牛版板所有
 */
public class Client {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        InetAddress ias = null;
        String talking = null;
        String receive = null;
        DatagramPacket dpt = null;
        DatagramSocket dst = null;
        byte[] bytes = new byte[1024];
        try {
            ias = InetAddress.getByName("localhost");
            while (true){
                System.out.print("请输入发送内容：");
                talking = input.next();
                dpt = new DatagramPacket(talking.getBytes(),talking.getBytes().length,ias,8000);
                dst = new DatagramSocket();
                dst.send(dpt);
                dpt.setData(bytes,0,1024);
                dst.receive(dpt);
                receive = new String(dpt.getData(),0,dpt.getLength());
                System.out.println("服务端说："+receive);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (dst != null){
                dst.close();
            }
        }

    }
}
